def parseConfig(dic,objKey):
    flag=1
    for key in dic:
        while('{' + key + '}' in dic[objKey]):
            flag=0
            break
    if flag==1:
        return dic[objKey]

    for key in dic:
        while('{' + key + '}' in dic[objKey]):
                val=parseConfig(dic,key)
                dic[objKey] = dic[objKey].replace('{' + key + '}', val)
    return dic[objKey]



def getConfigFile(lines, objKey):
    dic = {}
    for line in lines:
        line=line.strip()
        if(len(line) == 0):
            continue
        if(line[0] == '#'):
            continue
        # if('#' in line):
        #     line = line[0:line.index('#')]
        if('=' in line):
            key = line[0:line.index('=')].strip()
            value = line[line.index('=') + 1:].strip()
            # if('{' not in value and '}' not in value):
            #     if(key == objKey):
            #         return value
            
            dic[key] = value
        else:
            continue
    
    if(objKey not in dic):
        return None
    # ('{' in value and '}' in value):
    # for key in dic:
    #     while('{' + key + '}' in value):
    #         if('{'+objKey+'}' not in dic[key]):
    #             value = value.replace('{' + key + '}', dic[key])
    #         else:
    #             return -1
    value = dic[objKey]
    
    try:
        tv=(objKey)
        # ('{' in value and '}' in value):
        #print(dic)
        for key in dic:
            if('{' + key + '}' in dic[objKey]):
                print(tv)
                tv.append(key)
        print(tv)
       # print("1")
        for ekey in tv:
            if ekey==objKey:
                continue
            for key in dic:
                if('{' + key + '}' in dict[ekey] and key in tv):
                    raise Exception
        print(tv)
        dic=parseConfig(dic,objKey)
    except Exception as e:
        return -1
    return dic[objKey]
        


N = int(input())
lines = []
for i in range(N):
    lines.append(input())

key = input()

res = getConfigFile(lines, key)
if -1==res:
    print("ERROR")
elif None==res:
    print("NULL")
else:
    print(res)
